package com.cn.lyl.dao.impl;

import com.cn.lyl.dao.ServiceTypeDao;
import com.cn.lyl.model.ServiceTypeModel;
import com.cn.lyl.util.JDBC;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class ServiceTypeDaoImpl implements ServiceTypeDao {

    @Override
    public int add(ServiceTypeModel serviceTypeModel) {
        return 0;
    }

    @Override
    public int update(ServiceTypeModel serviceTypeModel) {
        return 0;
    }

    @Override
    public int delete(ServiceTypeModel serviceTypeModel) {
        return 0;
    }

    @Override
    public ServiceTypeModel findById(int id) {
        return null;
    }

    @Override
    public List<ServiceTypeModel> findAll(ServiceTypeModel serviceTypeModel) {
      return  null;
    }

    @Override
    public List<ServiceTypeModel> findAll() {
        String sql ="select type from service_type";
        ResultSet query = JDBC.query(sql);
        List<ServiceTypeModel> list = new ArrayList<>();
        try {
            while (query.next()) {
                ServiceTypeModel serviceTypeModel = new ServiceTypeModel();
                serviceTypeModel.setType(query.getString("type"));
                list.add(serviceTypeModel);
            }
        }catch (Exception e){

        }
        return list;
    }

    @Override
    public ServiceTypeModel findByName(ServiceTypeModel serviceTypeModel) {
        String sql ="select id from service_type where type ='"+serviceTypeModel.getType()+"'";
        ResultSet query = JDBC.query(sql);
        ServiceTypeModel serviceTypeModel1 = new ServiceTypeModel();
        try{
            if(query.next()){
                serviceTypeModel1.setId(query.getInt("id"));
                serviceTypeModel1.setType(query.getString("type"));
            }
        }catch (Exception e){

        }
        return serviceTypeModel1;
    }
}
